package com.atguigu.userprofile.app

import java.util.Properties

import com.atguigu.userprofile.ConstCode
import com.atguigu.userprofile.bean.TagInfo
import com.atguigu.userprofile.dao.TagInfoDao
import com.atguigu.userprofile.util.{ClickhouseUtil, MyPropertiesUtil}

import scala.collection.mutable.ListBuffer

object TaskBitmapApp {


//  1  建表 (后台完成)
//    由于无论标签增减变化 ， 根据标签值类型创建的四张表都不会发生变化，所以在后台命令行窗口一次性执行即可，不用再程序中建表。
//  2  数据的查询
//  3  数据的写入
//
//  insert  into xxx select xxx

  def main(args: Array[String]): Unit = {

    val taskId: String = args(0)
    val taskDate: String = args(1)
    // 根据标签值的类型把标签划分为四个集合
    // 分别插入到对应的bitmap表中
      val tagInfoList: List[TagInfo] = TagInfoDao.getTagListWithOn()
  // 根据标签值的类型 拆出四个集合
    val tagInfoStringList = new ListBuffer[TagInfo]()
    val tagInfoLongList = new ListBuffer[TagInfo]()
    val tagInfoDecimalList = new ListBuffer[TagInfo]()
    val tagInfoDateList = new ListBuffer[TagInfo]()
      for (tagInfo <- tagInfoList ) {
          if(tagInfo.tagValueType==ConstCode.TAG_VALUE_TYPE_STRING){
            tagInfoStringList.append(tagInfo)
          }else if(tagInfo.tagValueType==ConstCode.TAG_VALUE_TYPE_LONG){
            tagInfoLongList.append(tagInfo)
          }else if(tagInfo.tagValueType==ConstCode.TAG_VALUE_TYPE_DECIMAL){
            tagInfoDecimalList.append(tagInfo)
          }else if(tagInfo.tagValueType==ConstCode.TAG_VALUE_TYPE_DATE){
            tagInfoDateList.append(tagInfo)
          }
      }
    val sourceTableName="user_tag_merge_"+taskDate.replace("-","")

    insertBitmap(tagInfoStringList ,"user_tag_value_string",sourceTableName:String,taskDate:String )
    insertBitmap(tagInfoLongList ,"user_tag_value_long",sourceTableName:String,taskDate:String )
    insertBitmap(tagInfoDecimalList ,"user_tag_value_decimal",sourceTableName:String,taskDate:String )
    insertBitmap(tagInfoDateList ,"user_tag_value_date",sourceTableName:String,taskDate:String )



  }
  //  2  数据的查询 插入
  //    insert xxxxx
  //select tag_tuple.1 tag_code ,
  //tag_tuple.2 as tag_value,
  //groupBitmapState(uid)
  // from
  //(
  // select uid,
  // arrayJoin([('GENDER',gender),('AGEGROUP',agegroup),('FAVOR',favor)] ) tag_tuple
  //  -->>>arrayJoin([$tagvalueSQL] ) tag_tuple
  // from   $tableName
  // )  tv
  // group by tag_tuple.1,tag_tuple.2

  def insertBitmap(tagInfoList:ListBuffer[TagInfo],targetTableName:String,sourceTableName:String,taskDate:String ): Unit ={
   if(tagInfoList.size>0){
   // 把标签列表 转为SQL子句
    val tagInfoStringList: ListBuffer[String] = tagInfoList.map { tagInfo =>
      s"('${tagInfo.tagCode}',${tagInfo.tagCode.toLowerCase})"
    }
    //('GENDER',gender),('AGEGROUP',agegroup),('FAVOR',favor)
    val tagCodeSQL: String = tagInfoStringList.mkString(",")

     val properties: Properties = MyPropertiesUtil.load("config.properties")
     val upName: String = properties.getProperty("user-profile.dbname")


    val  insertSelectSQL=
      s"""
         |     insert  into $upName.$targetTableName
         |    select tag_tuple.1 tag_code ,
         |   tag_tuple.2 as tag_value,
         |   groupBitmapState(uid) , '$taskDate'
         |   from
         |  (
         |    select uid,
         |    arrayJoin([$tagCodeSQL] ) tag_tuple
         |    from    $upName.$sourceTableName
         |    )  tv
         |    where tag_tuple.2<>'' and tag_tuple.2 is not null
         |    group by tag_tuple.1,tag_tuple.2
       """.stripMargin

     val clearTableSQL=
       s"""
          | alter table $upName.$targetTableName delete where dt='$taskDate'
        """.stripMargin

    println(clearTableSQL)
    println(insertSelectSQL)

    ClickhouseUtil.executeSql(clearTableSQL)
    ClickhouseUtil.executeSql(insertSelectSQL)


   }
  }

}
